package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryInputOtherDetailBinding
import com.c.ctgproject.entity.guang.BatteryInpuOtherEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2021/9/22
 *描述:动力电池 补充信息 页面
 */
class BatteryInputOtherActivityDetailActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityBatteryInputOtherDetailBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val mData by lazy { intent.getSerializableExtra(MyConstants.DATA) as BatteryInpuOtherEntity.DataBean.ListBean }

    //图片处理
    private val imageShowList = ArrayList<String>()
    private val imageResultCode = 100


    override fun event() {
        mBinding.mPhotoNGV.init(this, imageShowList, 3, imageResultCode)
        if(mData.orderId==null){
            mBinding.bulkInOrderNo.isEnabled=true
        }
        //旧图片
        mData.picUrls?.let { images ->
            val resultList: MutableList<NineGridBean> = ArrayList()
            images.forEach { bean ->
                val nineGirdData = NineGridBean(bean)
                imageShowList.add(bean)
                resultList.add(nineGirdData)
            }
            mBinding.mPhotoNGV.addDataList(resultList)
        }

        mBinding.batteryCode.textSet(mData.batteryCode.handlerNull())
//        mBinding.count.textSet(mData.count.handlerNull())
        mBinding.weight.textSet(mData.weight.handlerNull())
        mBinding.capacityAh.textSet(mData.capacityAh.handlerNull())
        mBinding.voltage.textSet(mData.voltage.handlerNull())
        mBinding.capacityKwh.textSet(mData.capacityKwh.handlerNull())
        mBinding.stockPosi.textSet(mData.stockPosi.handlerNull())
        mBinding.mRemarkET.textSet(mData.remark.handlerNull())
        mBinding.bulkInOrderNo.textSet(mData.bulkInOrderNo.handlerNull())
    }

    override fun click() {
        mBinding.voltage.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.capacityAh.text.handlerNull())
            result?.let {
                mBinding.capacityKwh.textSet(result)
            }
        }
        mBinding.capacityAh.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.voltage.text.handlerNull())
            result?.let {
                mBinding.capacityKwh.textSet(result)
            }
        }
        mBinding.capacityKwh.handlerDecimal3()


        mBinding.mConfirmBTN.setOnClickListener {
            //所有本地文件的图片
            val filePathList = ArrayList<String>()
            imageShowList.forEach {
                if (!it.startsWith("http")) {
                    filePathList.add(it)
                }
            }

            if (filePathList.isNotEmpty()) {
                val fileList = ArrayList<File>()
                var index = 0
                filePathList.forEach {
                    index++
                    val file = File(it)
                    //图片压缩
//                            val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                    val newFile = ImageUtil.imageWaterMark(this, file, index)
                    val newFile: File = WaterRemark.handlerWaterRemark2File(this, file, index.toString())
                    fileList.add(newFile)
                }
                upDataFile(fileList) {
                    val dataList = ArrayList<String>()
                    it?.forEach { b ->
                        dataList.add(b.fileUrl.handlerNull())
                    }
                    MyLog.d("有新的文件")
                    //旧的也给他
                    imageShowList.forEach { bean ->
                        if (bean.startsWith("http")) {
                            dataList.add(bean)
                        }
                    }

                    submitToHttp(dataList)
                }
            } else {
                //没有新文件 但是有旧的url
                if (imageShowList.isNotEmpty()) {
                    MyLog.d("没有新文件给他旧url")
                    submitToHttp(imageShowList)
                } else {
                    MyLog.d("啥都没有")
                    submitToHttp(arrayListOf())
                }
            }
        }
    }

    private fun submitToHttp(imageList: ArrayList<String>) {
        val map = HashMap<String, Any?>()
        map["pbId"] = mData.pbId
        map["batteryCode"] = mBinding.batteryCode.text.handlerNull()
        map["bulkInOrderNo"]=mBinding.bulkInOrderNo.text.handlerNull()
//            map["count"] = mBinding.count.text.handlerNull()
        map["capacityAh"] = mBinding.capacityAh.text.handlerNull()
        map["voltage"] = mBinding.voltage.text.handlerNull()
        map["capacityKwh"] = mBinding.capacityKwh.text.handlerNull()
        map["remark"] = mBinding.mRemarkET.text.handlerNull()
        map["stockPosi"] = mBinding.stockPosi.text.handlerNull()
        map["weight"]=mBinding.weight.text.handlerNull()
        map["picUrls"] = imageList
        ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryInputOther(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
            MyLog.d("回调:$it")
            showToast("保存成功")
            finish()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    val resultList: MutableList<NineGridBean> = ArrayList()
                    list?.forEach { imageBean ->
                        val nineGirdData = NineGridBean(imageBean.imagePath)
                        imageShowList.add(imageBean.imagePath)
                        resultList.add(nineGirdData)
                    }
                    mBinding.mPhotoNGV.addDataList(resultList)
                }
                else -> {

                }
            }
        }
    }
}